package com.itboye.ihomebank.me;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.OrderViewPagerAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
/**
 * 我的生活管家
 *
 */
public class ActivityGuanJia extends BaseOtherActivity implements OnClickListener{
	ImageView close_icon;
	TextView add_shap_title_tv;
	
	ViewPager guanjia_viewpager;
	
	private ArrayList<Fragment> fragmentList;
	
	TextView guanjia_paidan;//派单中
	TextView guanjia_weixiu;//维修中
	TextView guanjia_pingjia;//待评价
	
	ImageView guanjia_line;
	private float offset;// 图片移动的偏移量
	private int currIndex;// 当前页卡编号
	@Override
	protected int layout_id() {
		return R.layout.activity_guan_jia;
	}
	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onInit() {
		add_shap_title_tv.setText("维修订单");
		initViewPager();
		initTextView();
		initImage();
		
		
	}
	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}
	}
	
	/**
	 * 初始化ViewPager
	 */
	private void initViewPager() {
		fragmentList=new ArrayList<Fragment>();
		
		ActivityMyGuanJia01 one=new ActivityMyGuanJia01();
		ActivityMyGuanJia02 two=new ActivityMyGuanJia02();
		ActivityMyGuanJia03 three=new ActivityMyGuanJia03();
		fragmentList.add(one);
		fragmentList.add(two);
		fragmentList.add(three);
		
		guanjia_viewpager.setAdapter(new OrderViewPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		
		guanjia_viewpager.setCurrentItem(0);// 设置当前显示标签页为第一页
		
		guanjia_viewpager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器
	}
	/**
	 * 初始化标签
	 */
	private void initTextView(){
		guanjia_paidan.setOnClickListener(new txListener(0));
		guanjia_weixiu.setOnClickListener(new txListener(1));
		guanjia_pingjia.setOnClickListener(new txListener(2));
		
	}
	private class txListener implements OnClickListener {
		private int index=0;  
		
		public txListener(int i) {  
            index =i;  
        } 
		@Override
		public void onClick(View v) {
			guanjia_viewpager.setCurrentItem(index);
		}
	}
	/**
	 * 初始化图片的位移像素
	 */
	private void initImage() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int screenH = dm.heightPixels;
		offset = screenW / 3.0f;
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				(int) offset, 2);
		guanjia_line.setLayoutParams(params);
	}
	/**
	 * 平移动画
	 * 
	 * @param currentItem
	 * @param durationMillis
	 */
	private void tranSlate(int currentItem, long durationMillis) {
		float one = offset;
		Log.v("offest", "" + offset);
		Animation animation = new TranslateAnimation(currIndex * one,
				currentItem * one, 0, 0);// 平移动画
		Log.v("currentoffest", "" + currIndex * one);
		Log.v("currentItem", "" + currentItem * one);
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(durationMillis);// 动画持续时间0.2秒
		guanjia_line.startAnimation(animation);// 是用ImageView来显示动画的
	}
	private class MyOnPageChangeListener implements OnPageChangeListener {
		private float one = offset;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int arg0) {

			tranSlate(arg0, 200);
			currIndex = arg0;
			
			if(arg0==0){
				//Toast.makeText(MyApplcation.ctx, "第1个", 0).show();
			}
			if(arg0==1){
				//Toast.makeText(MyApplcation.ctx, "第2个", 0).show();
			}
			if(arg0==2){
				//Toast.makeText(MyApplcation.ctx, "第3个", 0).show();
			}
		}
	}
	
}
